package com.yimi.yinhepda.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.pda.ym.utils.StringUtils;
import com.yimi.yinhepda.R;
import com.yimi.yinhepda.entity.response.LoadInfo;
import com.yimi.yinhepda.utils.Common;

import java.util.List;

/**
 * @author 000432
 */
public class LoadScanAdapter extends BaseAdapter {
    private LayoutInflater inflater;
    private List<LoadInfo> dataList;
    private Context context;

    public LoadScanAdapter(Context context, List<LoadInfo> dataList) {
        this.context = context;
        this.dataList = dataList;
        this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return null != dataList ? dataList.size() : 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup arg2) {
        if (dataList.isEmpty()) {
            return convertView;
        }
        LoadInfo item = dataList.get(position);
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.load_scan_item3, null);
            //运单号
            holder.waybillnum = convertView.findViewById(R.id.text1);
            //开单
            holder.count = convertView.findViewById(R.id.text3);
            //已扫
            holder.scaned = convertView.findViewById(R.id.text4);
            //库存
            holder.stock = convertView.findViewById(R.id.text5);
            //必走货类型
            holder.type = convertView.findViewById(R.id.lsi_tv_type);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        // 运单号
        holder.waybillnum.setText(item.getWaybillNo());
        //开单数
        holder.count.setText(String.format("%s", item.getSrcPiecesNum()));
        // 已扫
        if ("2".equals(item.getKyWaybillType()) && item.getScanCount() > 0) {
            holder.scaned.setText(String.format("%s", item.getSrcPiecesNum()));
        } else {
            holder.scaned.setText(String.format("%s", item.getScanCount()));
        }
        // 库存
        holder.stock.setText(String.format("%s", item.getPiecesNum()));
        if (item.getWaybillType() != null && item.getWaybillType() == 2) {
            //快递
            holder.type.setText(Common.getSecondType(Common.UC_WAYBILL_TYPE));
        } else if (StringUtils.isKyeYiMiWaybillNo(item.getWaybillNo())) {
            //跨越
            holder.type.setText(Common.getSecondType(Common.GOODSP_RIORITY_KUA_YUE));
        } else {
            //二级排序：获取产品类型
            int secondSort = item.getSecondSort();
            holder.type.setText(Common.getSecondType(secondSort));
        }

        switch (item.getType()) {
            case 0:
                convertView.setBackgroundColor(context.getResources().getColor(R.color.red));
                break;
            case 1:
                convertView.setBackgroundColor(context.getResources().getColor(R.color.white));
                break;
            case 2:
                convertView.setBackgroundColor(context.getResources().getColor(R.color.yellow));
                break;
            case 3:
                convertView.setBackgroundColor(context.getResources().getColor(R.color.green));
                break;
            default:
                break;
        }

        if (item.getWaybillType() != null && item.getWaybillType() == 2 && item.getSrcPiecesNum() == 1) {
            //快递类型运单
            if (item.getScanCount() > 0) {
                convertView.setBackgroundColor(context.getResources().getColor(R.color.green));
                holder.scaned.setText("1");
            }
        }
        return convertView;
    }

    static class ViewHolder {
        /**
         * 运单号
         */
        TextView waybillnum;
        /**
         * 开单
         */
        TextView count;
        /**
         * 扫描
         */
        TextView scaned;
        /**
         * 库存
         */
        TextView stock;
        /**
         * 必走货类型
         */
        TextView type;
    }

    @Override
    public Object getItem(int position) {
        // TODO 自动生成的方法存根
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO 自动生成的方法存根
        return position;
    }

    public void setDataList(List<LoadInfo> dataList) {
        this.dataList = dataList;
    }
}